package com.meishij;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.meishij.adapter.MallCategoryDetailAdapter;
import com.meishij.adapter.MallEndListAdapter;
import com.meishij.entry.MallCategoryEntry;
import com.meishij.server.MallServer;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MallCategoryActivity extends AppCompatActivity implements Callback<MallCategoryEntry> {
    private final String source="android";
    private final String format="json";
    private RecyclerView mall_category_details;
    private GridLayoutManager layoutManager;
    private MallCategoryDetailAdapter mallCategoryDetailAdapter;
    private TextView mall_category_details_title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall_category);
        mall_category_details = (RecyclerView) findViewById(R.id.mall_category_details);
        mall_category_details_title = (TextView) findViewById(R.id.mall_category_details_title);
        layoutManager = ((GridLayoutManager) mall_category_details.getLayoutManager());
        layoutManager.setSpanCount(2);
        Intent intent = getIntent();
        String cid1 = intent.getStringExtra("cid1");
        String goodsSource = intent.getStringExtra("goodsSource");
        String name = intent.getStringExtra("name");
        mall_category_details_title.setText(name);
        Retrofit build = new Retrofit.Builder()
                .baseUrl("http://api.meishi.cc")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<MallCategoryEntry> mallDetail = build.create(MallServer.class).getMallDetail(source, format, "1", "0", cid1, goodsSource, null, null);
        mallDetail.enqueue(this);
    }

    @Override
    public void onResponse(Call<MallCategoryEntry> call, Response<MallCategoryEntry> response) {
        MallCategoryEntry body = response.body();
        List<MallCategoryEntry.GoodsListBean> goods_list = body.getGoods_list();
        mallCategoryDetailAdapter = new MallCategoryDetailAdapter(this,goods_list);
        mall_category_details.setAdapter(mallCategoryDetailAdapter);
    }

    @Override
    public void onFailure(Call<MallCategoryEntry> call, Throwable t) {
        t.printStackTrace();
    }
}
